package kr.hkit.android_bookmgr.widget;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;

import kr.hkit.android_bookmgr.MainActivity;
import kr.hkit.android_bookmgr.R;
import kr.hkit.android_bookmgr.DAO.BookDAO;
import kr.hkit.android_bookmgr.DTO.BookData;
import kr.hkit.android_bookmgr.DTO.FinishedBook;
import kr.hkit.android_bookmgr.activity.BookListActivity;
import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;


public class BookWidget extends AppWidgetProvider {
	final static String ACTION_FAKENEWS_CHANGE = "FakeNewsChange";
	final static String PREF = "FakeNews";
	final static String TAG = "FakeNews";
	private static String widget;

	
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, 
			int[] appWidgetIds) {
		Log.d(TAG, "onUpdate, length = " + appWidgetIds.length + 
				", id = " + appWidgetIds[0]);
		for (int i = 0; i < appWidgetIds.length; i++) {
			UpdateNews(context, appWidgetManager, appWidgetIds[i]); 
		}
	}

	static void UpdateNews(Context context, AppWidgetManager appWidgetManager, 
			int widgetId) {
		
		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.bookwidget);
		BookDAO mBookMgr = BookDAO.getInstance(context);
		
		
		Calendar cal = new GregorianCalendar();
		int mMonth = cal.get(Calendar.MONTH);
		int thisMonth = mMonth + 1; // 이번달은 몇월인지
		
		SharedPreferences sharedPref = context.getSharedPreferences("PrefGoal", Activity.MODE_PRIVATE);
		int goalNum = sharedPref.getInt("goalNum", 0);
		
		ArrayList<FinishedBook> finishedBooks = mBookMgr.selectStatistic();
		for (int i = 0; i < finishedBooks.size(); i++) {
			if (finishedBooks.get(i).month == thisMonth) {
				widget = thisMonth+"월 : " + finishedBooks.get(i).count+"/" + goalNum + "권 (달성/목표)";
			}
		}
		
		views.setTextViewText(R.id.bookwidget_goal, widget);
		
	

		Intent intent = new Intent(context, MainActivity.class);
		PendingIntent pending = PendingIntent.getActivity(context, widgetId, 
				intent, PendingIntent.FLAG_UPDATE_CURRENT);
		views.setOnClickPendingIntent(R.id.bookwidget_mainlayout, pending); //어플 실행됨 

		Intent intent2 = new Intent(context, BookWidget.class);
		intent2.setAction(ACTION_FAKENEWS_CHANGE);
		intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
		PendingIntent pending2 = PendingIntent.getBroadcast(context, widgetId, intent2, 0);
		views.setOnClickPendingIntent(R.id.bookwidget_refresh, pending2); 

		appWidgetManager.updateAppWidget(widgetId, views);
	}

	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (action != null && action.equals(ACTION_FAKENEWS_CHANGE)) {
			int id = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
			UpdateNews(context, AppWidgetManager.getInstance(context), id);
			Log.d(TAG, "onReceive(CHANGE), id = " + id);
			return;
		}
		super.onReceive(context, intent);
	}

	public void onDeleted(Context context, int[] appWidgetIds) {
		for (int i = 0; i < appWidgetIds.length; i++) {
			Log.d(TAG, "onDeleted, id = " + appWidgetIds[i]);
			SharedPreferences prefs = context.getSharedPreferences(PREF, 0);
			SharedPreferences.Editor editor = prefs.edit();
			editor.remove("red_" + appWidgetIds[i]);
			editor.commit();
		}
	}

	public void onEnabled(Context context) {
		Log.d(TAG, "onEnabled");
	}

	public void onDisabled(Context context) {
		Log.d(TAG, "onDisabled");
	}
}
